/*
 * Copyright ©2018 贾维程. All rights reserved.
 */

package com.rtmart.imageupload.repositoryImp;

import com.rtmart.imageupload.entity.User;
import com.rtmart.imageupload.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserRepositoryImp implements UserRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //通过ID查用户对象
    @Override
    public User findById(Long id) {
        String sql = "select * from user where id=?";
        return jdbcTemplate.queryForObject(sql,new Object[]{id},new BeanPropertyRowMapper<>(User.class));
    }

    //通过用户名查用户对象，用于登录
    @Override
    public User findByUsername(String username) {
        String sql = "select * from user where username=?";
        return jdbcTemplate.queryForObject(sql,new Object[]{username},new BeanPropertyRowMapper<>(User.class));
    }

    //改名字
    @Override
    public Integer updateUser(String name, User user) {
        String sql = "update user set name=? where id=?";
        return jdbcTemplate.update(sql, name, user.getId());
    }

    //改密码
    @Override
    public Integer updatePassword(String password, User user) {
        String sql = "update user set password=? where id=?";
        return jdbcTemplate.update(sql, password, user.getId());
    }
}
